#!/usr/bin/env python

import os
import subprocess
CERNROOT=subprocess.call("root-config --exec-prefix >/dev/null 2>&1",stdin=None, stdout=None, stderr=None, shell=True)

if CERNROOT!=0:
  print "***ERROR***	ROOT has not been setup.EXITING "
  os.sys.exit(-1)
else:
  print "***INFO***	OK. CERNROOT DETECTED "
PROJECTDIR=os.getcwd()
EXTERNDIR=PROJECTDIR+"/external"
LIBCONFDIR=EXTERNDIR+"/libconfig-1.4.8"
GRLDIR=EXTERNDIR+"/GoodRunsLists-00-00-76"

#check paths are ok
#check project dir

if os.path.isdir(PROJECTDIR):
  print "***INFO***	DETECTED PROJECT DIRECTORY : "+PROJECTDIR
else:
  print "***ERROR***	PROJECT DIRECTORY : "+PROJECTDIR+" IS NOT OK. EXITING"
  os.sys.exit(-1)
#check libconfig dir first  
if os.path.isdir(LIBCONFDIR):
  print "***INFO***	DETECTED LIBCONFIG DIRECTORY : "+LIBCONFDIR
else:
  print "***ERROR***	LIBCONFDIR DOES NOT EXIST IN : "+EXTERNDIR+". EXITING"
  os.sys.exit(-1)
#check GRL dir 
if os.path.isdir(GRLDIR):
  print "***INFO***	DETECTED GRL DIRECTORY : "+GRLDIR
else:
  print "***ERROR***	GRL DIRECTORY : "+GRLDIR+"IS NOT OK. EXITING"
  os.sys.exit(-1)
#now check already compiled libraries
LIBCONFOK=os.path.isfile(LIBCONFDIR+"/lib/libconfig++.so")
GRLOK=os.path.isfile(GRLDIR+"/StandAlone/GoodRunsListsLib.so")

if LIBCONFOK:
  print "***INFO***	LIBCONFIG LIBRARY ALREADY EXISTS. SKIP COMPILATION"

if GRLOK:
  print "***INFO***	GRL LIBRARY ALREADY EXISTS. SKIP COMPILATION"
#now compile EXTERNAL PACKAGES

if not GRLOK:
  print "***INFO***	COMPILING GRL"
  os.chdir(GRLDIR)
  if not os.path.isdir(GRLDIR+"/cmt"):
    print "GRLDIR DOES NOT CONTAIN CMT DIR... EXITING"
    os.sys.exit(-1)
  else:
    os.chdir("cmt")
    grlcode=subprocess.call("make -f Makefile.Standalone >/dev/null 2>&1",shell=True)
    if grlcode !=0:
      print "**ERROR**	Cannot compile GRL package... EXITING"
      os.sys.exit(-1)
    else:
      print "***INFO***	GRL COMPILED SUCCESFULLY"
      print "***INFO***	CLEANING UP GRL OBJ CODES"
      os.chdir(GRLDIR+"/cmt")
      grl_cleancode=subprocess.call("make -f Makefile.Standalone clean",shell=True)
      if grl_cleancode!=0:
	print "***WARNING***	WHILE CLEANING UP GRL DIRECTORY"

#now compile libconfig

if  not LIBCONFOK:
  os.chdir(LIBCONFDIR)
  libconf_confcode=subprocess.call("./configure --prefix="+LIBCONFDIR+">/dev/null 2>&1",shell=True)
  if libconf_confcode!=0:
    print "***ERROR***	LIBCONFIG DID NOT CONFIGURE BEFORE COMPILATION... EXITING"
    os.sys.exit(-1)
  else:
    print "***INFO***	LIBCONFIG CONFIGURED. NOW ATTEMPTING TO COMPILE"
    libconf_makecode=subprocess.call("make > /dev/null 2>&1",shell=True)
    if libconf_makecode!=0:
      print "***ERROR***	LIBCONFIG COULD NOT BE COMPILED... EXITING"
    else:
	print "***INFO***	LIBCONFIG COMPILED SUCCESFULLY. NOW ATTEMPTING TO INSTALL"
	libconf_instcode=subprocess.call("make install > /dev/null 2>&1",shell=True)
	if libconf_instcode!=0:
	  print "***ERROR***	LIBCONF COULD NOT BE INSTALLED... EXITING"
	else:
	  print "***INFO***	LIBCONFIG INSTALLED"
	  libconf_cleancode=subprocess.call("make clean >/dev/null 2>&1",shell=True)
	  if libconf_cleancode!=0:
	    print "***WARNING***	CLEANING UP LIBCONF FAILED"
	  else:
	    print "***INFO***	CELANED UP LIBCONF PACKAGE..."
print "\n"
print "***INFO***	SHARED LIBRARY LOCATIONS FOR EXTERNAL PACKAGES:"
print "libconfig	:"+LIBCONFDIR+"/lib"
print "GoodRunLists	:"+GRLDIR+"/StandAlone"
print "\n"
print "***INFO	ALL OK"
print "***INFO you can now compile&link analyses via \"make\" utilities"